<template>
  <div class="event-keycodes" v-keyUp:[keyDown]>
    <div v-if="isShow">
      <div v-for="key in keyObj" :key="key">
        <p>event.{{ key }}</p>
        <p>{{ model[key] }}</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  directives: {
    keyUp: {
      bind(el, options) {
        document.body.onkeydown = (e) => {
          options.arg(e);
        };
      },
    },
  },
  data() {
    return {
      keyObj: ["key", "keyCode", "code"],
      model: {},
    };
  },
  methods: {
    keyDown({ key, keyCode, code, ctrlKey, shiftKey }) {
      let model = { key, keyCode, code };
      if (shiftKey) {
        model.key = "shift+" + model.key;
      }
      if (ctrlKey) {
        model.key = "ctrl+" + model.key;
      }
      this.model = model;
    },
  },
  computed: {
    isShow() {
      let status = this.keyObj.find((item) => !this.model[item]);
      if (status) {
        return false;
      }
      return true;
    },
  },
};
</script>

<style lang="scss" scoped>
.event-keycodes {
  width: 100%;
  height: 100%;
  background-color: #e1e1e1;
  display: flex;
  justify-content: center;
  align-items: center;
  & > div > div {
    text-align: center;
    margin: 0 30px;
    width: 150px;
    & > p:nth-child(1) {
      color: rgb(85, 85, 85);
      font-size: 14px;
      margin-bottom: 15px;
    }
    & > p:last-child {
      display: block;
      width: 100%;
      border: 1px solid #000;
      background-color: #fff;
      padding: 20px 0;
      color: rgb(85, 85, 85);
      font-size: 20px;
      font-weight: bold;
    }
  }
}
</style>